package com.bw.controller;

import com.bw.domain.Community;
import com.bw.domain.State;
import com.bw.service.OrderService;
import com.bw.service.StateService;
import com.bw.utils.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author :Beyond🎸🎸
 * @date : 2025-03-11 21:19:45
 * @description Rock and Roll
 **/
@Controller
@RequestMapping("state")
public class StateController {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    StateService stateService;
@RequestMapping("tolist")
public String tolist(){
    return "state/list";
}

@RequestMapping("list")
@ResponseBody
public R list(State state,Integer page,Integer limit){
    PageHelper.startPage(page,limit);
    List<State> list = stateService.list(state);
    PageInfo<State> info = new PageInfo<>(list);
    return R.OK(info);
}
    @RequestMapping("listSell")
    @ResponseBody
    public R listSell(){
//    先查询redis缓存，入过没有再查询数据库 使用list结构

       List<State> stateList = redisTemplate.opsForList().range("stateList",0,-1);
//        如果缓存没有
        if(stateList==null||stateList.size()==0){
            stateList = stateService.listSell();
//            将数据存入缓存
            redisTemplate.opsForList().rightPushAll("stateList",stateList);
        }
        return R.OK(stateList);
    }
    @RequestMapping("listNew")
    @ResponseBody
    public R listNew(){

  List<State> stateList =  stateService.listNew();
  return R.OK(stateList);
    }
}
